import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';

import {persistCombineReducers} from 'redux-persist';
import storage from 'redux-persist/es/storage'; // default: localStorage if web, AsyncStorage if react-native

import  ReducersStore from '../reducers';
// 创建一个 Redux store 来以存放应用中所有的 state，应用中应有且仅有一个 store。
let store;
const config = {
    key: 'redux',
    storage,
};
if (!(window.__REDUX_DEVTOOLS_EXTENSION__ || window.__REDUX_DEVTOOLS_EXTENSION__)) {
    store = createStore(
        persistCombineReducers(config, ReducersStore),
        applyMiddleware(thunk)
    );
} else {
    store = createStore(
        persistCombineReducers(config, ReducersStore),
        compose(applyMiddleware(thunk),window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()) // 插件调试，未安装会报错
    );
}

export default store;
